package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /*
    * 新增菜品
    * */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //向菜品表加入一条数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        Long id = dish.getId();//获取insert生成的id
        List<DishFlavor> flavors = dishDTO.getFlavors();
       if(flavors!=null&&flavors.size()>0){
           flavors.forEach( dishFlavor ->
                   dishFlavor.setDishId(id));
           //向口味表加入数据
           dishFlavorMapper.insertBatch(flavors);
       }
    }
    /*
    *   菜品分页查询
    * */
    @Override
    public PageResult pageQuary(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page= dishMapper.pageQuary(dishPageQueryDTO);
        long total = page.getTotal();
        List<DishVO> result = page.getResult();
        return new PageResult(total,result);
    }
    /*
    * 批量删除菜品
    * */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断当前菜品是否能够删除,是否存在起售中菜品
        for (Long id : ids) {
            Dish dish= dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                //菜品起售中不能删除
                throw  new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断当前菜品是否能够删除,是否关联套餐
       List<Long> setmealIds=  setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds!=null &&setmealIds.size()>0){
            throw  new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品表中的数据
        for (Long id : ids) {
           dishMapper.deleteById(id);
           dishFlavorMapper.deleteByDishId(id); //删除菜品表关联的口味
        }

    }
    /*
    * 根据Id查询菜品
    * */
    @Override
    public DishVO getById(Long id) {
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        List<DishFlavor> dishFlavor=dishFlavorMapper.getDishFlavorById(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }
  /*
  *  修改菜品
  * */
    @Override
    @Transactional
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update(dish);
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach( dishFlavor ->
                    dishFlavor.setDishId(dishDTO.getId()));
            //向口味表加入数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }
  /*
  * 启用,禁用菜品
  * */
    @Override
    @Transactional
    public void startOrstop(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
        // 如果是停售操作，还需要将包含当前菜品的套餐也停售
        if(status==StatusConstant.DISABLE){
            List<Long> dishIds= new ArrayList<>();
            dishIds.add(id);
            List<Long>   setmealids  = setmealDishMapper.getSetmealIdsByDishIds(dishIds);
            if(setmealids!=null &&setmealids.size()>0){
                for (Long setmealid : setmealids) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealid)
                            .status(StatusConstant.DISABLE).build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }
   /*
   * 根据分类id查询菜品
   * */
    @Override
    public List<Dish> list(Long categoryId) {
        /*List<Dish> list=dishMapper.list(categoryId);
        return list;*/
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
/*
*根据分类id查询菜品及其口味
* */
    @Override
    public List<DishVO> getDishWithFlavors(Long categoryId) {
        List<DishVO> dishVOS=new ArrayList<>();
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> dishes = dishMapper.list(dish);
        for (Dish d : dishes) {
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(d,dishVO);
            List<DishFlavor> dishFlavor = dishFlavorMapper.getDishFlavorById(d.getId());
            dishVO.setFlavors(dishFlavor);
            dishVOS.add(dishVO);
        }
        return dishVOS;
    }
}
